(ns json-rpc.http.client
  (:require clojure.data.json))


(defn http-post [url data]
  (let [url (java.net.URL. url)
        connection (doto (.openConnection url)
                     (.setDoOutput true)
                     (.setRequestProperty "Content-Type" "application/json"))]
    (org.apache.commons.io.IOUtils/write data (.getOutputStream connection) "UTF-8")
    (org.apache.commons.io.IOUtils/toString (.getInputStream connection) "UTF-8")))


(defn json-rpc-call [url method params]
  (let [response (clojure.data.json/read-json (http-post url (clojure.data.json/json-str {:method method :params params :id nil})) false)
        {result "result" error "error"} response]
    (when error
      (throw (Exception. error)))
    result))

